package com.cos.controller;

import com.cos.commons.R;
import com.cos.exception.vo.BusinessException;
import com.cos.fegin.api.UserService;
import com.cos.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user-back")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/{page}.html")
    public String page(@PathVariable("page") String page) {
        return "user/"+page;
    }
    @RequestMapping("/add")
    @ResponseBody
    public R add(User user){//这里接受的是vo 主要是表单传入
        boolean add = userService.add(user);
       // int i=10/0;
//        if (1==1){
//            throw  new BusinessException(333,"输入信息出现异常");
//        }
        return R.write(add);
    }
    @RequestMapping("/getList")
    public String getList (Model model){
        List<User> list = userService.getList();
        model.addAttribute("userList",list);
        model.addAttribute("user",new User());
        return "user/userList";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestParam(value = "id") Integer id){
        boolean delete = userService.delete(id);
        return R.write(delete);
    }

    @RequestMapping("/datadel")
    @ResponseBody
    public R datadel(String id){
        boolean datadel = userService.datadel(id);
        return R.write(datadel);
    }

    @RequestMapping("/getUserList")
    public String getUserList(User user, ModelMap modelMap){

        List<User> userList = userService.getUserList(user);
        modelMap.put("user",user);
        modelMap.put("userList",userList);
        return "user/userList";
    }

    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id,ModelMap modelMap){
        User user = userService.toUpdate(id);
        modelMap.put("user",user);
        return "/user/updateUser";
    }
    @RequestMapping("/update")
    @ResponseBody
    public R toUpdate(User user){
        System.out.println(user);
        boolean update = userService.update(user);
        return R.write(update);
    }
}
